<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="单据编码">
                <a-input v-model="queryParam.applyCode" placeholder="请输入单据编码" allow-clear />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="请假天数">
                <a-input v-model="queryParam.leaveDay" placeholder="请输入请假天数" allow-clear />
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="开始时间">
                  <a-date-picker
                    placeholder="请选择"
                    v-model="queryParam.leaveBegin"
                    valueFormat="YYYY-MM-DD"
                    style="width: 100%"
                    allow-clear></a-date-picker>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="结束时间">
                  <a-date-picker
                    placeholder="请选择"
                    v-model="queryParam.leaveEnd"
                    valueFormat="YYYY-MM-DD"
                    style="width: 100%"
                    allow-clear></a-date-picker>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="流程状态">
                  <a-select placeholder="请选择" v-model="queryParam.workflowStatus" style="width: 100%" allow-clear>
                    <a-select-option v-for="(d, index) in workflowStatusOptions" :key="index" :value="d.dictValue">
                      {{ d.dictLabel }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="请假人">
                  <a-input v-model="queryParam.createUserName" placeholder="请输入请假人" allow-clear />
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 操作 -->
      <div class="table-operations">
        <a-button type="primary" @click="$refs.createForm.showForm()" v-hasPermi="['apply:leave:add']">
          <a-icon type="plus" />
          新增
        </a-button>
        <a-button
          type="primary"
          :disabled="single"
          @click="handleUpdate(undefined,selectedRowKeys)"
          v-hasPermi="['apply:leave:edit']">
          <a-icon type="edit" />
          修改
        </a-button>
        <a-button type="danger" :disabled="multiple" @click="handleDelete" v-hasPermi="['apply:leave:delete']">
          <a-icon type="delete" />
          删除
        </a-button>
        <a-button type="primary" @click="$refs.importExcel.importExcelHandleOpen()" v-hasPermi="['apply:leave:import']">
          <a-icon type="import" />
          导入
        </a-button>
        <a-button type="primary" @click="handleExport" v-hasPermi="['apply:leave:export']">
          <a-icon type="download" />
          导出
        </a-button>
        <a-button
          type="dashed"
          shape="circle"
          :loading="loading"
          :style="{float: 'right'}"
          icon="reload"
          @click="handleQuery" />
      </div>

      <create-form
        ref="createForm"
        @ok="handleQuery"
      />

      <!-- 上传文件 -->
      <import-excel
        ref="importExcel"
        :excelForm="excelForm"
        @ok="handleQuery"
      />

      <!-- 数据展示 -->
      <a-table
        :loading="loading"
        rowKey="applyId"
        :columns="columns"
        :data-source="dataSource"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :pagination="false">

        <span slot="workflowStatus" slot-scope="text, record">
          {{ workflowStatusFormat(record) }}
        </span>
        <span slot="operation" slot-scope="text, record">
          <a @click="handleUpdate(record, undefined)" v-hasPermi="['apply:leave:edit']" v-show="record.workflowStatus === 'draft'">
            <a-icon type="edit" />
            修改
          </a>
          <a-divider type="vertical" v-hasPermi="['apply:leave:delete']" v-show="record.workflowStatus === 'draft'"/>
          <a @click="handleDelete(record)" v-hasPermi="['apply:leave:delete']" v-show="record.workflowStatus === 'draft'">
            <a-icon type="delete" />
            删除
          </a>
          <a-divider type="vertical" v-hasPermi="['apply:leave:edit']" v-show="record.workflowStatus === 'draft'"/>
          <a @click="handleView(record)" v-hasPermi="['apply:leave:query']">
            <a-icon type="search" />
            查看
          </a>
        </span>
      </a-table>
      <!-- 分页 -->
      <a-pagination
        class="ant-table-pagination"
        show-size-changer
        show-quick-jumper
        :current="queryParam.pageNum"
        :total="total"
        :page-size="queryParam.pageSize"
        :showTotal="total => `共 ${total} 条`"
        @showSizeChange="onShowSizeChange"
        @change="changeSize"
      />
    </a-card>
  </page-header-wrapper>
</template>
<script>
import { deleteLeave, listLeave } from '@/api/demo/apply/leave'
import createForm from './modules/createForm'
import ImportExcel from '@/views/system/user/modules/ImportExcel.vue'
import { mapGetters } from 'vuex'

export default {
  name: 'Leave',
  components: {
    ImportExcel,
    createForm
  },
  props: {},
  data () {
    return {
      // 表格数据
      dataSource: [],
      // 选择的keys
      selectedRowKeys: [],
      // 选择的行
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // loading
      loading: false,
      // 总数
      total: 0,
      // 流程状态
      workflowStatusOptions: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        applyCode: undefined,
        leaveDay: undefined,
        leaveBegin: undefined,
        leaveEnd: undefined,
        workflowStatus: undefined,
        createUserName: undefined
      },

      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          align: 'center',
          customRender: (text, record, index) => `${(this.queryParam.pageNum - 1) * this.queryParam.pageSize + index + 1}`,
          width: 60
        },
        {
          title: '单据名称',
          dataIndex: 'applyName',
          align: 'center'
        },
        {
          title: '单据编码',
          dataIndex: 'applyCode',
          align: 'center'
        },
        {
          title: '请假天数',
          dataIndex: 'leaveDay',
          align: 'center'
        },
        {
          title: '开始时间',
          dataIndex: 'leaveBegin',
          scopedSlots: { customRender: 'leaveBegin' },
          align: 'center'
        },
        {
          title: '结束时间',
          dataIndex: 'leaveEnd',
          scopedSlots: { customRender: 'leaveEnd' },
          align: 'center'
        },
        {
          title: '请假原因',
          dataIndex: 'leaveReason',
          align: 'center'
        },
        {
          title: '审批状态',
          dataIndex: 'workflowStatus',
          scopedSlots: { customRender: 'workflowStatus' },
          align: 'center'
        },
        {
          title: '请假人',
          dataIndex: 'createUserName',
          align: 'center'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          scopedSlots: { customRender: 'createTime' },
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          align: 'center'
        }
      ],
      excelForm: {
        templateUrl: 'oa/leave/excel/template',
        fileName: '请假申请导入模板',
        importUrl: 'oa/leave/excel/import'
      }
    }
  },
  created () {
    this.handleQuery()
    this.getDicts('biz_status').then(response => {
      this.workflowStatusOptions = response.data
    })
  },
  computed: {
    ...mapGetters(['userId'])
  },
  watch: {},
  methods: {
    /** 查询 */
    handleQuery () {
      this.loading = true
      listLeave(this.queryParam).then(res => {
          if (res.code === 200) {
            this.dataSource = res.data.rows
            this.total = res.data.total
            this.loading = false
          }
        }
      )
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        applyCode: undefined,
        leaveDay: undefined,
        leaveBegin: undefined,
        leaveEnd: undefined,
        workflowStatus: undefined,
        createUserName: undefined
      }
      this.handleQuery()
    },
    // 分页数量
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.handleQuery()
    },
    // 选择分页
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.handleQuery()
    },
    // 选择表格数据
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    // 展开收起
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    /** 更新按钮操作 */
    handleUpdate (row, ids) {
      const applyId = row ? row.applyId : ids
      this.$refs.createForm.showForm(applyId)
    },
    /** 删除按钮操作 */
    handleDelete (record) {
      var that = this
      const ids = record.applyId || this.selectedRowKeys
      this.$confirm({
        title: '温馨提示',
        content: '确认删除所选中数据?',
        onOk () {
          return deleteLeave(ids)
            .then(() => {
              that.onSelectChange([], [])
              that.handleQuery()
              that.$message.success(
                '删除成功',
                3
              )
            })
        },
        onCancel () {
        }
      })
    },
    /** 查看按钮操作 */
    handleView (record) {
      this.$router.push({ path: '/leave-apply-view', query: { id: record.applyId } })
    },
    // 字典翻译
    workflowStatusFormat (row) {
      return this.selectDictLabel(this.workflowStatusOptions, row.workflowStatus)
    },
    /** 导出按钮操作 */
    handleExport () {
      var that = this
      this.$confirm({
        title: '是否确认导出?',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk () {
          that.download('/apply/leave/excel/export', {
            ...that.queryParam
          }, `请假申请信息.xlsx`)
        },
        onCancel () {
        }
      })
    }
  }
}
</script>
